package com.test.kotlin.ui.adapter

import android.annotation.SuppressLint
import android.graphics.Rect
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.View.GONE
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.mtjsoft.www.gridviewpager_recycleview.GridViewPager
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.LayoutHelper
import com.alibaba.android.vlayout.VirtualLayoutManager
import com.alibaba.android.vlayout.layout.GridLayoutHelper
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.flyco.tablayout.SlidingTabLayout
import com.test.kotlin.R
import com.test.kotlin.bean.*
import com.test.kotlin.ui.widget.TitleTabLayout
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.indicator.CircleIndicator

class MainHomeAdapter :DelegateAdapter.Adapter<MainHomeAdapter.MainHomeViewHolder>{
    var viewType=0
    var layoutHelper:LayoutHelper?=null
    var viewCount=0

    var bannerData:MutableList<HomeBanner>?=null

    var hGap=SizeUtils.dp2px(10f)

    var dataType=0
    var recommendData:MutableList<HomeRmtj>?=null
    var topicData:MutableList<SpecialTopicData>?=null

    var title:String?=null

    var newsData:MutableList<NewsData>?=null

    var liveData:MutableList<HomeLive>?=null

    var vodData:MutableList<HomeVod>?=null

    var activityData:MutableList<HomeActivity>?=null

    var venueData:MutableList<HomeVenue>?=null

    var serviceData:MutableList<HomeCultureOrderdishe>?=null

    var appData:MutableList<HomeCultureApp>?=null

    var categoryData:MutableList<String>?=null
    var listener:TitleTabLayout.OnTabItemSelectListener?=null

    //外部链接
    var linkData:MutableList<LinkData>?=null
    var isLookMore=true


    constructor(type:Int,count:Int,helper: LayoutHelper){
        this.viewType=type
        this.viewCount=count
        this.layoutHelper=helper
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainHomeViewHolder {
        var holder:MainHomeViewHolder?=null
        var view:View?=null
        when(viewType){
            0x0001 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_banner_item,parent,false)
            0x0002 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_menu_item,parent,false)
            0x0003 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_title_item,parent,false)
            0x0004,0x0041,0x0042 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_horizontal_scroll_item,parent,false)
            0x0005 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_title_more_item,parent,false)
            0x0006 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_news_item,parent,false)
            0x0007 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_live_item,parent,false)
            0x0008 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_live_big_item,parent,false)
            0x0009 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_av_space_item,parent,false)
            0x0010 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_activity_item,parent,false)
            0x0011 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_service_item,parent,false)
            0x0012 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_special_apply_item,parent,false)
            0x0013 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_relate_link_itme,parent,false)
            0x0014 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_link_item,parent,false)
            0x0015 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_link_more_item,parent,false)
            0x0016 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_link_type_choose_item,parent,false)
            0x0017 -> view=LayoutInflater.from(parent.context).inflate(R.layout.layout_home_link_title_item,parent,false)
        }
        holder=view?.let {

            if(viewType==0x0004){
                MainHomeViewHolder(it,viewType,dataType,hGap)
            }else{
                MainHomeViewHolder(it,viewType)
            }
        }
        if(viewType==0x0013) {
            holder?.tabLayout?.setOnTabItemSelectListener(listener)
        }
        return holder!!
    }

    override fun getItemCount(): Int {
        return viewCount
    }

    override fun getItemViewType(position: Int): Int {
        if(viewType==0x0007&&position==0) return viewType+1


        if(viewType==0x0014&&viewCount>=6&&position==viewCount-1){
            if(viewCount==6&&isLookMore){
                return viewType+1
            }else if(viewCount>6){
                return viewType+1
            }
        }
        return viewType
    }

    override fun onCreateLayoutHelper(): LayoutHelper {
        return layoutHelper!!
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MainHomeViewHolder, position: Int) {
        when(viewType){
            0x0001 -> {

                holder.bannerAdapter?.updateData(bannerData)
                holder.banner?.start()
            }
            0x0004,0x0041,0x0042 -> {
                when(viewType){
                    0x0004 -> {
                        //精彩推荐
                        recommendData?.apply {
                            holder.recommendAdapter.data=this
                            holder.recommendAdapter.notifyDataSetChanged()
                        }
                    }
                    0x0041 -> {
                        //专题集锦
                        topicData?.apply {
                            holder.topicAdapter?.data=this
                            holder.topicAdapter?.notifyDataSetChanged()
                        }
                    }
                    0x0042 -> {
                        //场馆导航
                        venueData?.apply {
                            holder.venueAdapter?.data=this
                            holder.venueAdapter?.notifyDataSetChanged()
                        }
                    }
                }
            }
            0x0005 -> {
                title?.apply {
                    holder.tvTitle?.text=this
                }
            }
            0x0006 ->{
                newsData?.let {
                    with(it[position]){
                        if(createTime.contains("-")){
                            var times=createTime.split("-")
                            if(times.size>=3){
                                holder.tvNewsDay.text=times[0]
                                holder.tvNewsTime.text= "${times[1]}-${times[2]}"
                            }else{
                                holder.tvNewsDay.text=createTime
                            }
                        }
                        holder.tvNewsTitle.text=title
                    }
                }
            }
            0x0007 -> {
                liveData?.let {
                    with(it[position]){
                        Glide.with(holder.ivLive)
                            .load(posterImg)
                            .placeholder(R.drawable.place_holder)
                            .into(holder.ivLive)

                        holder.tvLiveTitle.text=recommendName
                        holder.tvLiveCount.text=viewCount.toString()

                    }
                }
            }
            0x0009 -> {
                vodData?.let{
                    with(it[position]){
                        Glide.with(holder.ivAVSpace)
                            .load(vod?.posterImg)
                            .placeholder(R.drawable.place_holder)
                            .into(holder.ivAVSpace)

                        holder.tvAVSpaceTitle.text=vod?.recommendName
                    }
                }
            }
            0x0010 -> {
                activityData?.let {
                    with(it[position]){
                        Glide.with(holder.ivActivityImg)
                            .load(posterImg)
                            .placeholder(R.drawable.place_holder)
                            .into(holder.ivActivityImg)

                        holder.tvActivityTitle.text=recommendName
                        holder.tvActivityAddress.text=address
                        holder.tvActivityTime.text="${startDate}至$endDate"
                    }
                }
            }
            0x0011 -> {
                serviceData?.let {
                    with(it[position]){
                        Glide.with(holder.ivServerImg)
                            .load(service?.posterImg)
                            .centerCrop()
                            .placeholder(R.drawable.place_holder)
                            .into(holder.ivServerImg)

                        holder.tvServerTitle.text=service?.recommendName
                        holder.tvServerPublisher.text=service?.pubOrgName
                    }
                }
            }
            0x0012 -> {
                appData?.let {
                    with(it[position]){
                        Glide.with(holder.ivAppImg)
                            .load(cultureapp?.posterImg)
                            .placeholder(R.drawable.place_holder)
                            .centerCrop()
                            .into(holder.ivAppImg)

                        holder.tvAppName.text=cultureapp?.app_name
                        holder.tvAppDes.text=cultureapp?.app_desc

                    }
                }
            }
            0x0013 -> {
                categoryData?.let {
                    holder.tabLayout.setTabList(it)
                }
            }
            0x0014 -> {
                var type=getItemViewType(position)
                when(type){
                    0x0014 -> {
                        linkData?.let {
                            if(position<it.size){
                                with(it[position]) {
                                    Glide.with(holder.ivLinkImg)
                                        .load(file_root)
                                        .placeholder(R.drawable.place_holder)
                                        .into(holder.ivLinkImg)
                                }
                            }
                        }
                    }
                    0x0015 -> {
                        holder.tvLinkMore.text=if(isLookMore) "查看更多" else "收起"

                        holder.tvLinkMore.setOnClickListener {
                            if(isLookMore){
                                linkData?.let {
                                    viewCount=it.size+1
                                    isLookMore=false
                                    notifyItemRangeInserted(6,viewCount-6)
                                }
                            }else{
                                var count=viewCount-6
                                viewCount=6
                                isLookMore=true
                                notifyItemRangeRemoved(6,count)

                            }
                        }
                    }
                }

//                linkData?.let {
//                    LogUtils.e("设置外链数据...")
//                    holder.linkTypeAdapter.updateLinkData(it)
//                }
            }

        }
    }

    fun updateNewsData(datas:MutableList<NewsData>?){
        datas?.let {
            viewCount=it.size
            newsData=it
            notifyItemRangeChanged(0,viewCount)
        }
    }

    fun updateLiveData(datas:MutableList<HomeLive>?){
        datas?.let {
            viewCount=it.size
            liveData=it
            notifyItemRangeChanged(0,viewCount)
        }
    }

    fun updateVodData(datas:MutableList<HomeVod>?){
        datas?.let {
            viewCount=it.size
            vodData=it
            notifyItemRangeChanged(0,viewCount)
        }
    }

    fun updateActivityData(datas:MutableList<HomeActivity>?){
        datas?.let {
            viewCount=it.size
            activityData=it
            notifyItemRangeChanged(0,viewCount)
        }
    }

    fun updateServerData(datas:MutableList<HomeCultureOrderdishe>?){
        datas?.let {
            viewCount=it.size
            serviceData=it
            notifyItemRangeChanged(0,viewCount)
        }
    }

    fun updateApplyData(datas:MutableList<HomeCultureApp>?){
        datas?.let {
            viewCount=it.size
            appData=it
            notifyItemRangeChanged(0,viewCount)
        }
    }

    fun updateLinkData(datas:MutableList<LinkData>?){
        datas?.let {
            viewCount=if(it.size>6){
                isLookMore=true
                6
            } else it.size
            linkData=it
            notifyDataSetChanged()

//            notifyItemChanged(0)
        }
    }

    class MainHomeViewHolder :RecyclerView.ViewHolder{
        var banner: Banner<HomeBanner,HomeBannerAdapter>?=null
        var bannerAdapter:HomeBannerAdapter?=null
        var gridPager:GridViewPager?=null

        lateinit var rv:RecyclerView
        var hGap=SizeUtils.dp2px(10f)
        var dataType=0
        lateinit var recommendAdapter:HomeRecommendAdapter
        lateinit var topicAdapter: SpecialTopicAdapter
        lateinit var venueAdapter:VenueAdapter

        //lateinit var delegateAdapter: DelegateAdapter
        lateinit var linkAdapter: HomeLinkAdapter

        var tvTitle: TextView?=null

        lateinit var tvNewsDay:TextView
        lateinit var tvNewsTime:TextView
        lateinit var tvNewsTitle:TextView

        lateinit var tvLiveTitle:TextView
        lateinit var tvLiveState:TextView
        lateinit var tvLiveCount:TextView
        lateinit var ivLive:ImageView

        lateinit var tvAVSpaceTitle:TextView;
        lateinit var ivAVSpace:ImageView

        lateinit var ivActivityImg:ImageView
        lateinit var tvActivityTitle:TextView
        lateinit var tvActivityState:TextView
        lateinit var tvActivityPeople:TextView
        lateinit var tvActivityAddress:TextView
        lateinit var tvActivityDistance:TextView
        lateinit var ratingBar:RatingBar
        lateinit var tvActivityViewCount:TextView
        lateinit var tvActivityTime:TextView

        lateinit var ivServerImg:ImageView
        lateinit var tvServerTitle:TextView
        lateinit var tvServerPublisher:TextView
        lateinit var tvServerFocus:TextView

        lateinit var ivAppImg:ImageView
        lateinit var tvAppName:TextView
        lateinit var tvAppDes:TextView
        lateinit var ivAppFav:ImageView
        lateinit var tvAppFav:TextView

        lateinit var ivLink:ImageView
        lateinit var tabLayout:TitleTabLayout

        lateinit var ivLinkImg:ImageView
        lateinit var tvLinkMore:TextView

        lateinit var gvLink: GridView
        lateinit var linkTypeAdapter:LinkAdapter

        constructor(view:View,type:Int):super(view){
            initView(type)
        }

        constructor(view:View,type:Int,dataType:Int,hGap:Int) : super(view) {
            this.dataType=dataType
            this.hGap=hGap
            initView(type)
        }

        private fun initView(type:Int) {

            when(type){
                0x0001->{
                    banner=itemView.findViewById(R.id.banner)
                    banner?.layoutParams?.width=ScreenUtils.getScreenWidth()
                    banner?.layoutParams?.height=ScreenUtils.getScreenWidth()*9/16
                    banner?.requestLayout()
                    banner?.indicator= CircleIndicator(itemView.context)

                    bannerAdapter= HomeBannerAdapter(arrayListOf())

                    banner?.adapter= bannerAdapter!!
                }
                0x0002-> {
                    gridPager=itemView.findViewById(R.id.gvp_menu)

                    var titles=itemView.resources.getStringArray(R.array.home_menus)

                    gridPager?.setDataAllCount(titles.size)
                    gridPager?.setImageTextLoaderInterface { imageView, textView, position ->
                        textView.text=titles[position]
                        imageView.setImageResource(R.mipmap.home_venue_icon)
                    }?.show()

                }
                0x0004,0x0041,0x0042 -> {
                    rv=itemView.findViewById(R.id.rv)
                    rv?.let {
                        var layoutManager=LinearLayoutManager(itemView.context)
                        layoutManager.orientation=LinearLayoutManager.HORIZONTAL

                        it.layoutManager=layoutManager

                        it.addItemDecoration(object : RecyclerView.ItemDecoration() {
                            override fun getItemOffsets(
                                outRect: Rect,
                                view: View,
                                parent: RecyclerView,
                                state: RecyclerView.State
                            ) {
                                super.getItemOffsets(outRect, view, parent, state)
                                var childPosition=parent.getChildAdapterPosition(view)
                                when(childPosition){
                                    0 ->{
                                        outRect.left=SizeUtils.dp2px(10f)
                                        outRect.right=hGap/2
                                    }
                                    parent.adapter?.itemCount?.minus(1) ->{
                                        outRect.left=hGap/2
                                        outRect.right=SizeUtils.dp2px(10f)
                                    }
                                    else -> {
                                        outRect.left=hGap/2
                                        outRect.right=hGap/2
                                    }
                                }
                            }
                        })

                        when(type){
                            0x0004 -> {
                                recommendAdapter= HomeRecommendAdapter()
                                rv.adapter=recommendAdapter
                            }
                            0x0041 -> {
                                topicAdapter= SpecialTopicAdapter()
                                rv.adapter=topicAdapter
                            }
                            0x0042 -> {
                                venueAdapter= VenueAdapter();
                                rv.adapter=venueAdapter
                            }
                        }
                    }
                }
                0x0005 -> {
                    tvTitle=itemView.findViewById(R.id.tv_title_more)
                }
                0x0006 -> {
                    tvNewsDay=itemView.findViewById(R.id.tv_news_day)
                    tvNewsTime=itemView.findViewById(R.id.tv_news_time)
                    tvNewsTitle=itemView.findViewById(R.id.tv_news_title)
                }
                0x0007,0x0008 -> {
                    ivLive=itemView.findViewById(R.id.iv_live)
                    tvLiveTitle=itemView.findViewById(R.id.tv_live_title)
                    tvLiveState=itemView.findViewById(R.id.tv_live_state)
                    tvLiveCount=itemView.findViewById(R.id.tv_live_view_count)
                }
                0x0009 ->{
                    ivAVSpace=itemView.findViewById(R.id.iv_av_space)
                    tvAVSpaceTitle=itemView.findViewById(R.id.tv_av_space_title)
                }
                0x0010 -> {
                    ivActivityImg=itemView.findViewById(R.id.iv_activity_img)
                    tvActivityTitle=itemView.findViewById(R.id.tv_activity_title)
                    tvActivityState=itemView.findViewById(R.id.tv_activity_state)
                    tvActivityAddress=itemView.findViewById(R.id.tv_activity_address)
                    tvActivityDistance=itemView.findViewById(R.id.tv_activity_distance)
                    tvActivityPeople=itemView.findViewById(R.id.tv_activity_peoples)
                    tvActivityPeople.visibility=GONE
                    tvActivityTime=itemView.findViewById(R.id.tv_activity_time)
                    tvActivityViewCount=itemView.findViewById(R.id.tv_activity_view_count)
                    ratingBar=itemView.findViewById(R.id.rb_activity_rating)
                }
                0x0011 -> {
                    ivServerImg=itemView.findViewById(R.id.iv_server_img)
                    tvServerTitle=itemView.findViewById(R.id.tv_server_title)
                    tvServerPublisher=itemView.findViewById(R.id.tv_server_publisher)
                    tvServerFocus=itemView.findViewById(R.id.tv_server_focus)

                    ivServerImg.layoutParams.width=(ScreenUtils.getScreenWidth() - SizeUtils.dp2px(25f)) / 5 * 2
                    ivServerImg.requestLayout()
                }
                0x0012 -> {
                    ivAppImg=itemView.findViewById(R.id.iv_apply_img)
                    tvAppName=itemView.findViewById(R.id.tv_apply_name)
                    tvAppDes=itemView.findViewById(R.id.tv_apply_des)
                    tvAppFav=itemView.findViewById(R.id.tv_apply_fav)
                    ivAppFav=itemView.findViewById(R.id.iv_apply_fav)

                    ivAppImg.layoutParams.width=(ScreenUtils.getScreenWidth() - SizeUtils.dp2px(75f)) / 3
                    ivAppImg.requestLayout()
                }
                0x0013 -> {
                    ivLink=itemView.findViewById(R.id.iv_transaction)
                    tabLayout=itemView.findViewById(R.id.tab_layout)
                }
                0x0014,0x0015-> {
                    itemView.layoutParams.width=(ScreenUtils.getScreenWidth()- SizeUtils.dp2px(32f))/3
                    itemView.layoutParams.height=itemView.layoutParams.width/2
                    itemView.requestLayout()
                    when(type){
                        0x0014 -> ivLinkImg=itemView as ImageView
                        0x0015 -> tvLinkMore=itemView as TextView
                    }

//                    rv=itemView.findViewById(R.id.rv)
//                    rv.layoutParams.width=ViewGroup.LayoutParams.MATCH_PARENT
//                    rv.layoutParams.height=SizeUtils.dp2px(200f)
//                    rv.requestLayout()
//
//                    var layoutManager: GridLayoutManager=object :GridLayoutManager(itemView.context,3){
//                        override fun canScrollVertically(): Boolean {
//                            return false
//                        }
//                    }
//                    rv.layoutManager=layoutManager
//
//                    linkAdapter=HomeLinkAdapter(0x0001,0)
//                    rv.adapter=linkAdapter

//                    gvLink=itemView.findViewById(R.id.gv_link)
//                    linkTypeAdapter=LinkAdapter(itemView.context)
//                    gvLink.adapter=linkTypeAdapter


                }
            }
        }
    }
}